package cn.edu.zjicm.wordsnet_d.view;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.net.Uri;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference;
import cn.edu.zjicm.wordsnet_d.db.WordFactory;
import cn.edu.zjicm.wordsnet_d.util.Utils;

public class CreateShareBitmap {

	private View view;
	private Activity context;
	private int background;
	private int dayPunchOuted;
	private int numsTodayLearned;

	private String strChinese;
	private String strEnglish;

	private String dateStr;
	private Utils.createBitmapMode mode;

	public CreateShareBitmap(Activity context, Utils.createBitmapMode mode) {
		this.context = context;
		this.mode = mode;
		initDatas();
		init();
	}

	public View getView() {
		return view;
	}

	private void initDatas() {
		int bgs[] = new int[] { R.drawable.share_bg1, R.drawable.share_bg2, R.drawable.share_bg3, R.drawable.share_bg4, R.drawable.share_bg5, R.drawable.share_bg6, R.drawable.share_bg7,
				R.drawable.share_bg8, R.drawable.share_bg9, R.drawable.share_bg10, R.drawable.share_bg11, R.drawable.share_bg12, };
		background = bgs[(int) (Math.random() * 12)];
		String[] shareContentEn = new String[] { "", "", "", "Cease to struggle and you cease to live.", "I am a slow walker,but I never walk backwards",
				"There is but one secret to sucess--never give up!", "", "" };
		String[] shareContentCh = new String[] { "逼格不再是拿着手机到处约，而是用来进化成学霸", "你排队发呆的时候，我正掏出手机背单词", "一天又要过去了，是不是感觉离梦想又近了一步？", "生命不止，奋斗不息", "我走得很慢，但是我从来不会后退", "成功只有一个秘诀——永不放弃！",
				"无论怎样的花朵，总会藏有芬芳，无论是什么季节，都会有阳光", "一路沿途的美丽风景参托着所有努力的化身" };
		int ContentRandomIndex = (int) (Math.random() * shareContentEn.length);
		strChinese = shareContentCh[ContentRandomIndex];
		strEnglish = shareContentEn[ContentRandomIndex];

		dayPunchOuted = WordFactory.getInstance(context).getPunchOutTimes();
		numsTodayLearned = AppPreference.getTodayStudyNewWordCount(context) + AppPreference.getTodayReviewWordCount(context);

		SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
		Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
		dateStr = formatter.format(curDate);
	}

	private void init() {
		view = context.getLayoutInflater().inflate(R.layout.view_share_image, null);

		if (background != 0) {
			RelativeLayout bg = (RelativeLayout) view.findViewById(R.id.background);
			bg.setBackgroundResource(background);
		}
		LinearLayout resetSize = (LinearLayout) view.findViewById(R.id.reset_size);
		LayoutParams layoutParams = new LayoutParams(Utils.getScreenWidth(context), LayoutParams.WRAP_CONTENT);
		resetSize.setLayoutParams(layoutParams);

		String headPortraitURL = AppPreference.getHeadPortrait(context);
		if (headPortraitURL != null) {
			CircleImageView avatar = (CircleImageView) view.findViewById(R.id.user_avatar);
			Uri imageUri = Uri.parse(headPortraitURL);
			avatar.setImageURI(null);
			avatar.setImageURI(imageUri);
		}

		String nickNameStr = AppPreference.getNickName(context);
		if (nickNameStr == null) {
			nickNameStr = AppPreference.getLoginID(context);
		}
		TextView nickname = (TextView) view.findViewById(R.id.user_nick_name);
		nickname.setText(nickNameStr);

		TextView daysUntilNow = (TextView) view.findViewById(R.id.days_punch_out);
		TextView wordsTodayLearned = (TextView) view.findViewById(R.id.words_nums_today_learned);
		TextView strCh = (TextView) view.findViewById(R.id.str_ch);
		TextView strEng = (TextView) view.findViewById(R.id.str_en);
		TextView date = (TextView) view.findViewById(R.id.date);

		TextView dayUntilNowTitle = (TextView) view.findViewById(R.id.days_punch_out_title);
		TextView wordsTodayLearnedTitle = (TextView) view.findViewById(R.id.words_nums_today_learned_title);
		TextView divideRight = (TextView) view.findViewById(R.id.divide_right);
		TextView unLearnedWords = (TextView) view.findViewById(R.id.words_unlearned);
		LinearLayout layoutRight = (LinearLayout) view.findViewById(R.id.linearlayout_right);

		if (mode == Utils.createBitmapMode.forMainActivity) {
			dayUntilNowTitle.setText("生词");
			wordsTodayLearnedTitle.setText("熟词");

			int unfamiliarWordsNum1 = 0;
			for (int i = 1; i <= WordFactory.wordReviewTime.length - 2; i++) {
				unfamiliarWordsNum1 += AppPreference.getNewWordsFmDegree(context, i);
			}
			int familiarWordsNum1 = AppPreference.getNewWordsFmDegree(context, WordFactory.wordReviewTime.length - 1);
			int unlearnedWordsNum1 = AppPreference.getNewWordsFmDegree(context, 0);

			daysUntilNow.setText(unfamiliarWordsNum1 + "");
			wordsTodayLearned.setText(familiarWordsNum1 + "");
			unLearnedWords.setText(unlearnedWordsNum1 + "");

			divideRight.setVisibility(View.VISIBLE);
			layoutRight.setVisibility(View.VISIBLE);
			date.setVisibility(View.GONE);
		} else {
			dayUntilNowTitle.setText("已打卡天数");
			wordsTodayLearnedTitle.setText("今天学习单词");
			divideRight.setVisibility(View.GONE);
			layoutRight.setVisibility(View.GONE);

			daysUntilNow.setText(dayPunchOuted + "");
			wordsTodayLearned.setText(numsTodayLearned + "");
			date.setText(dateStr);
		}
		if (strEnglish.length() > 0)
			strEng.setText(strEnglish);
		else
			strEng.setVisibility(view.GONE);
		if (strChinese.length() > 0)
			strCh.setText(strChinese);
		else
			strCh.setVisibility(view.GONE);

	}
}
